<?php
defined('IN_TS') or die('Access Denied.');

/* 
 * 秀店话题内容页
 */

$topicid = intval($_GET['topicid']);
if($topicid){
$nid = $db->once_fetch_assoc("select * from ".dbprefix."group_topics where topicid < $topicid order by topicid desc limit 0,1");
$bfid = $db->once_fetch_assoc("select * from ".dbprefix."group_topics where topicid > $topicid order by topicid asc limit 0,1");
$nexttopicid =	$nid['topicid'];
$bftopicid = $bfid['topicid'];
}
if($topicid == 0){
	header("Location: ".SITE_URL);
}

$new['group']->isTopic($topicid);

$strTopic = $new['group']->getOneTopic($topicid);
	
//宝贝分类
if($strTopic['typeid'] != '0'){
	$strTopic['type'] = $db->once_fetch_assoc("select * from ".dbprefix."group_topics_type where typeid='".$strTopic['typeid']."'");
}

// 宝贝收藏数

$strTopic['collects_num'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where topicid=".$topicid."");

// 宝贝收藏数

$strTopic['tacollects_num'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where userid=".$strTopic['userid']."");

$followedUsersum = $db->once_num_rows("select userid from ".dbprefix."user_follow where userid_follow=".$strTopic['userid']."");

$count_follow = $db->once_num_rows("select userid from ".dbprefix."user_follow where userid=".$strTopic['userid']."");

//秀店
$strGroup = $db->once_fetch_assoc("select * from ".dbprefix."group where groupid='".$strTopic['groupid']."'");

//判断会员是否加入该秀店
$groupid = intval($strGroup['groupid']);
$userid = intval($TS_USER['user']['userid']);

$isGroupUser = $db->once_num_rows("select * from ".dbprefix."group_users where userid='$userid' and groupid='$groupid'");

//是否关注
if($TS_USER['user']['userid'] != '' && $TS_USER['user']['userid'] != $strUser['userid']){
	$followNum = $db->once_num_rows("select * from ".dbprefix."user_follow where userid='".$TS_USER['user']['userid']."' and userid_follow='$userid'");
	if($followNum > '0'){
		$strUser['isfollow'] = true;
	}else{
		$strUser['isfollow'] = false;
	}
}else{
	$strUser['isfollow'] = false;
}
//浏览方式
if($strGroup['isopen']=='1' && $isGroupUser=='0'){
	
	$title = $strTopic['title'];
	include template("topic_isopen");
	
}else{
	
	
	//宝贝标签
	$strTopic['tags'] = aac('tag')->getObjTagByObjid('topic','topicid',$topicid);
	$strTopic['content'] = editor2html($strTopic['content'],$topicid);
	$cimg = $strTopic['content'];
	if (strstr($cimg,'<img ') && strstr($cimg,'taobao')){
preg_match_all("/.*src=\"([^^]*?)\".*/i",$cimg,$match);
	$img310 = $match[1][0]."_310x310.jpg";
 $pic64d =SITE_URL."pic/".base64_encode($img310).".jpg";
 $pic64 = str_replace('=','',$pic64d);
 $strTopic['content310'] = $cimg;
 }
 	if (strstr($cimg,'<img ') && strstr($cimg,'taobao')){
preg_match_all("/.*src=\"([^^]*?)\".*/i",$cimg,$match);
 $pic64d =SITE_URL."pic/".base64_encode($match[1][0]).".jpg";
 $pic64 = str_replace('=','',$pic64d);
 $strTopic['contentd'] = $cimg;
 }
	
	$strTopic['user']	= aac('user')->getUserForApp($strTopic['userid']);
	$strTopic['user']['signed'] = hview($strTopic['user']['signed']);
	$title = $strTopic['title'];
	
	/**获取评论列表*/
	$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
	$url = SITE_URL.tsurl('group','topic',array('topicid'=>$topicid,'page'=>''));
	$arrTopicComment = $new['group']->getGroupContentComment($page,15,$topicid);
	
	
	
	$groupContentCommentNum = $new['group']->getGroupContentCommentNum('topicid',$topicid);
	
	$pageUrl = pagination($groupContentCommentNum, 15, $page, $url);
	
	//判断会员是否加入该秀店
	$userid = intval($TS_USER['user']['userid']);
	$isGroupUser = $db->once_num_rows("select * from ".dbprefix."group_users where userid='$userid' and groupid='".$strTopic['groupid']."'");
	//判断会员是否收藏该话题
	$iscollect = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where userid='$userid' and topicid = '$topicid'");
	
	$groupid = $strTopic['groupid'];
	
	//秀店成员
	$strGroupUser = $db->once_fetch_assoc("select * from ".dbprefix."group_users where userid='$userid' and groupid='".$strTopic['groupid']."'");
	
	//最分享子
	$newTopics = $db->fetch_all_assoc("select topicid,userid,title from ".dbprefix."group_topics where groupid='$groupid' and isshow='0' order by addtime desc limit 6");
	
	foreach($newTopics as $key=>$item){
		$newTopic[] = $item;
		$newTopic[$key]['user'] = aac('user')->getSimpleUser($item['userid']);
	}
	$dangqianuser['user'] =aac('user')->getUserForApp($TS_USER[user][userid]);
	
	
	//相关宝贝
	foreach($strTopic['tags'] as $key=>$item){
		$str .=$item[tagid].",";
	}
	$tagid = $str;
$strTag = $db->once_fetch_assoc("select * from ".dbprefix."tag where tagid in ('$tagid')");

$arrTagId = $db->fetch_all_assoc("select * from ".dbprefix."tag_topic_index where tagid in ('$tagid')");

$topic_num = $db->once_fetch_assoc("select count(topicid) from ".dbprefix."tag_topic_index where tagid in ('$tagid')");

foreach($arrTagId as $item){
	$strTopictag = $db->once_fetch_assoc("select topicid,userid,groupid,title,count_comment,count_view,isphoto,isattach,addtime,uptime from ".dbprefix."group_topics where topicid = '".$item['topicid']."' and topicid <>'$topicid' order by count_view desc limit 8");
	$arrTopicstag[] = $strTopictag;
}
$arrTopicstag =array_filter($arrTopicstag ); 
$arrTopicstag = array_slice($arrTopicstag,'-8');
if( is_array($arrTopicstag)){
		foreach($arrTopicstag as $key=>$item){
			$thumbimg=$db->once_fetch_assoc("select * from ".dbprefix."thumb_topic_topic where topicid=".$item['topicid']."");
			if ($thumbimg&&strstr($thumbimg['indeximgurl'],'t100x100')){
			$arrTopicstag[$key]['thumb'] =$thumbimg['indeximgurl'];
			}else{
			$arrTopicstag[$key]['thumb'] ="/";
			}
		}
	}
	
	
	($page > 1) ? $titlepage = " - 第".$page."页" : $titlepage='';
	
	$title = $title.$titlepage;
	
	
	$topicid=intval($_GET['topicid']);
	$gourl=$db->once_fetch_assoc("select * from ".dbprefix."go_url where topicid=".$topicid."");
	$purlg = $gourl['gourl'];
	if (strstr($purlg,'taobao.com')||strstr($purlg,'tmall.com')){
			$istaobao =1;
			}else {
			$istaobao =0;
			}
	include template('topic');
	
	$db->query("update ".dbprefix."group_topics set `count_view`=count_view+1 where topicid='".$topicid."'");
	
}